package com.theo.service.impl;

import com.theo.entity.CityInfo;
import com.theo.entity.DailyEpidemicInfo;
import com.theo.entity.EpidemicDetailInfo;
import com.theo.entity.EpidemicInfo;
import com.theo.mapper.CityInfoMapper;
import com.theo.mapper.EpidemicInfoMapper;
import com.theo.service.EpidemicInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 方涛
 * @since 2021-02-02
 */
@Service
public class EpidemicInfoServiceImpl extends ServiceImpl<EpidemicInfoMapper, EpidemicInfo> implements EpidemicInfoService {
    @Autowired
    private EpidemicInfoMapper epidemicInfoMapper;
    @Autowired
    private CityInfoMapper cityInfoMapper;

    /***
     * 保存当日的疫情数据，返回还未录入数据的省份列表
     * @param dailyEpidemicInfo
     * @return
     */
    @Override
    public List<CityInfo> saveEpidemicInfo(DailyEpidemicInfo dailyEpidemicInfo) {
        System.out.println("进入service");
        String date = dailyEpidemicInfo.getDate();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date1 = null;
        try {
            date1 = sdf.parse(date);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        System.out.println("日期"+date1);
        for (EpidemicInfo epidemicInfo : dailyEpidemicInfo.getArray()) {
            epidemicInfo.setDate(date1);
            //System.out.println(epidemicInfo.getCityId()+"-"+epidemicInfo.getAffirmed());
            epidemicInfoMapper.insert(epidemicInfo);
        }
        return cityInfoMapper.findNoDataProvinces(date);
    }

    @Override
    public List<EpidemicDetailInfo> findLastestData() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        return epidemicInfoMapper.findLastestData(format);
    }

    @Override
    public List<EpidemicDetailInfo> findLastestGlobalData() {
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);
        return epidemicInfoMapper.findLastestGlobalData(format);
    }

    @Override
    public List<EpidemicDetailInfo> findAllData(String date) {
        return epidemicInfoMapper.findAllData(date);
    }

    @Override
    public List<EpidemicInfo> findAll() {
        return epidemicInfoMapper.findAll();
    }


    @Override
    public List<EpidemicInfo> findAllByDate(String date) {
        return epidemicInfoMapper.findAllByDate(date);
    }

    @Override
    public EpidemicInfo queryEpidemicInfoById(Integer id) {
        return epidemicInfoMapper.queryEpidemicInfoById(id);
    }

    @Override
    public void deleteDataById(Integer id) {
        epidemicInfoMapper.deleteById(id);
    }

    @Override
    public void updateData(EpidemicInfo epidemicInfo) {
        epidemicInfoMapper.updateById(epidemicInfo);
    }

    @Override
    public List<EpidemicInfo> selectDataForChart() {
        return epidemicInfoMapper.selectDataForChart();
    }

    @Override
    public List<EpidemicInfo> selectDataForGlobalChart() {
        return epidemicInfoMapper.selectDataForGlobalChart();
    }
}
